﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;


public class OC_ScrollUIForItem : RecieaveInputObject
{
    CharacterInfoScene charscene;

    enum SortMode { 
        Equip,
        Materil,
        Useable
    }

    const float AniTime = 0.3f;

    const float ListGap = 300.0f;
    const float PopupGap = 540.0f;
    const float ScrollFirstItemOrizninY = 307.0f;
    const float ScrollViewOrizinY = -125.0f;
    const float ScrollViewOrizinOffsetY = 0.0f;
    
    public Transform m_StatRoot;  //stat 루트 (아이템 선택시 스텟 변경)

    public GameObject m_BlankText;  //아이템 없다는 메세지출력
        
    Item m_SelectItem = null;
    Item m_SelectItemForEnhance = null;

    Transform[] m_InfoList;

    bool m_IsInit = false;      // 초기화 되있는지 여부 (동적 초기화)

    int m_PreSelectListNum = -1;
    int m_SelectListNum = -1;   


    bool m_IsOffsetMoving = false;
    bool m_IsStatBGMoving = false;
    float m_OffsetGoal = 0;
    float m_OffsetCrr = 0;
    float m_OffsetTime = 0;
    float m_TimeGap = 0;


    const float ItemListStartY = 307.0f;
    const float ItemListGapY = 300.0f;

    public GameObject m_ItemListItem;
    List<Oc_ItemListItem> m_ItemList = new List<Oc_ItemListItem>();

    #region 스텟 정보창 관련 변수
    UILabel[] m_StatLabels = new UILabel[7];
    UILabel[] m_StatNameLabels = new UILabel[7];
    GameObject[] m_BigStatLabels;
    const float StatLabelStartY = 340.0f;
    const float StatLabelLineGapY = 74.0f;    
    const float StatLabelBigGapY = 50.0f;
    #endregion

    #region 아이템 정렬 관련 변수

    public UILabel[] m_SortKindsLabel;
    public Animation m_ItemSortAni;

    bool m_IsSortMenuOn = false;
    SortKind m_SortKind = SortKind.Level;
    SortMode m_SortMode = SortMode.Equip;

    string tempString0 = null;
    string tempString1 = null;
    string tempString2 = null;

    List<Item> m_SortedItem = new List<Item>();
    public bool IsHaveItem()
    {
        return m_SortedItem.Count != 0;
    }

    #endregion
    
    public UILabel m_ItemTotalNum;
    public UILabel m_ItemNum;   

    public OC_ItemPanelController m_ItemPanelController; // 상위 컨트롤러 할당

    int ItemMax = 0;

    public override void AwakeObject()
    {      
        foreach (DConstNumber t in GameData.Inst.constNumber)
        {
            if (t.name.Equals("ITEM_NUM_MAX"))
            {
                ItemMax = t.value;
                break;
            }
        }

        tempString0 = TextController.GetStringWithId("uiItemInvenPutOnLv");
        tempString1 = TextController.GetStringWithId("uiCommonTextRarityDo");
        tempString2 = TextController.GetStringWithId("uiCommonTextEnhance");

        m_SortKindsLabel[0].text = tempString0;
        m_SortKindsLabel[1].text = tempString1;
        m_SortKindsLabel[2].text = tempString2;

        m_ItemTotalNum.text = ItemMax.ToString();
    }

    UISprite m_StatBG;
    void StartItemList()
    {
        UIScrollView sv = GetComp("UIScrollView") as UIScrollView;
        if (sv == null)
            sv = AddComp("UIScrollView") as UIScrollView;
       
        m_Cam = GameObject.Find("UI Root/Camera").camera;

        m_BigStatLabels = new GameObject[4];
        for(int i = 0 ; i < m_BigStatLabels.Length ; i++)
        {
            m_BigStatLabels[i] = m_StatRoot.Find(string.Format("StatOn{0:00}", i + 1)).gameObject;
        }

        m_StatBG = m_StatRoot.Find("square").GetComponent("UISprite") as UISprite;
    }


    public void UpdateItemsList(MemberModel selectModel, bool isNeedItemReset)  // 아이템 리스트 업데이트
    {
        charscene = CharacterInfoScene.Instance as CharacterInfoScene;

        if (!m_IsInit)
        {
            StartItemList();
            m_IsInit = true;
            isAbleTouch = true;
        }

        charscene.m_SelectMember = selectModel;
        if (isNeedItemReset)
        {
            m_SelectItem = null;
        }

        SetSortMenuUI();        // 아이템 정렬 
                 
        UpdateItemInfoUI();     // 리스트 업데이트 하면 아이템은 무조건 비선택

        int itemNum = m_SortedItem.Count;

        GameObject tempItem = null;
        Oc_ItemListItem item = null;             

        for (int i = 0; i < itemNum; i++)
        {
            if (m_ItemList.Count <= i)
            {
                if (i == 0)
                    tempItem = m_ItemListItem;
                else
                {
                    tempItem = Instantiate(m_ItemListItem) as GameObject;
                    tempItem.transform.parent = m_ItemListItem.transform.parent;
                    tempItem.transform.localScale = m_ItemListItem.transform.localScale;
                    tempItem.transform.localRotation = m_ItemListItem.transform.localRotation;
                    Vector3 pos = m_ItemListItem.transform.localPosition;
                    pos.y = ItemListStartY - (i * ItemListGapY);
                    tempItem.transform.localPosition = pos;
                }

                tempItem.name = string.Format("Item_List{0:00}", i);

                item = tempItem.GetComponent("Oc_ItemListItem") as Oc_ItemListItem;
                m_ItemList.Add(item);
            }

            m_ItemList[i].SetItem(m_SortedItem[i], charscene.m_SelectMember);
        }

        m_BlankText.SetActive(false);
        
        if (m_SortedItem.Count < 1)       //리스트 없을때 m_ItemListItem 변수에 할당 안됨
        {
            m_ItemListItem.SetActive(false);
            m_BlankText.SetActive(true);
        }

        //if (isNeedItemReset)
        {           
            for (int i = 0; i < m_ItemList.Count; i++)
            {
                m_ItemList[i].gameObject.SetActive(i < itemNum);
                m_ItemList[i].SetOn(false); // 처음엔 무조건 닫힌 상태
            }

            m_SelectListNum = -1;   //리스트 위치 갱신
            MoveList(true);
        }
    }

    IEnumerator EndOffsetMoveToWithDelay(float time)
    {
        yield return new WaitForSeconds(time);

        EndOffsetMoveTo();
    }

    // 스크롤뷰 이동 끝난후 터치 인식
    void EndOfScrollViewMoving()
    {
        isAbleTouch = true;
        UIScrollView sv = GetComp("UIScrollView") as UIScrollView;
        sv.restrictWithinPanel = true; 
    }
   
    // 스크롤뷰 이동 트윈후에 불리는 함수 (스크롤뷰 오프셋 수정 마무리)
    void EndOffsetMoveTo()
    {        
        m_IsOffsetMoving = false;
        EndOfScrollViewMoving();
    }

    // 스크롤뷰 이동 트윈중에 동작하는 함수 (스크롤뷰 오프셋 수정)
    void OffsetMove()
    {        
        UIScrollView sv = GetComp("UIScrollView") as UIScrollView;
        SpringPanel sp = GetComponent("SpringPanel") as SpringPanel;
        if (sp != null)        
            sp.enabled = false;
       
        UIPanel up = GetComp("UIPanel") as UIPanel;
        Vector2 offSet = up.clipOffset;
        offSet.y = ScrollViewOrizinOffsetY - sv.transform.localPosition.y + ScrollViewOrizinY;
        up.clipOffset = offSet;
    }


    public override void ButtonTouch(Collider collider)  // 버튼 터치 (터치 시작 끝 같은 오브젝트일때 터치 끝 시점에 호출)
    {
        if (collider.name.Contains("Button_ItemSort_"))
        {
            if (m_IsSortMenuOn)
            {
                string buttonName = collider.gameObject.name.Replace("Button_ItemSort_", "");
                int num = System.Convert.ToInt32(buttonName);
                m_SortKind = (SortKind)(((int)m_SortKind + num) % 3);   // 각 상황마다 대응
                UpdateItemsList(charscene.m_SelectMember, true);               
                m_ItemSortAni.Play("sort_close");
            }
            else
            {
                m_ItemSortAni.Play("sort_open");
            }
            m_IsSortMenuOn = !m_IsSortMenuOn;
        }
        else
        {
            switch (collider.gameObject.name)
            {
                case "Button_ItemSort":
                    {
                        if (m_IsSortMenuOn)
                            m_ItemSortAni.Play("sort_close");
                        else
                            m_ItemSortAni.Play("sort_open");

                        m_IsSortMenuOn = !m_IsSortMenuOn;
                    }
                    break;
                case "Button_ItemListMore": //
                    {
                        for (int i = 0; i < m_ItemList.Count; i++)
                        {
                            if (m_ItemList[i].transform == collider.transform.parent.parent)
                            {
                                m_PreSelectListNum = m_SelectListNum;
                                m_SelectListNum = i;
                                break;
                            }
                        }

                        MoveList(false);
                        
                        //(지스타임시)퀘스트 인디케이터 가리기
                        QuestManager.instance.HideTouchIndicator();
                    }
                    break;
                case "Button_ItemListClose":
                    {
                        m_SelectListNum = -1;

                        MoveList(false);
                    }
                    break;
                case "Button_ItemSale":
                    {
                        //C2S_ITEM_SELL
                        Oc_ItemListItem tOc = collider.transform.parent.parent.GetComponent("Oc_ItemListItem") as Oc_ItemListItem;
                        m_SelectItem = tOc.m_Item;  // 펼치지 않고 판매 누르면 셀렉트 아이템이 없으므로 판매 버튼시는 아이템 m_SelectItem 에 할당

                        Popup_TwoButton sc = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;
                        sc.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextSell"),
                            string.Format(TextController.GetStringWithId("uiPopupItemInventoryCaution2"), TextController.GetItemName(tOc.m_Item)));
                        sc.SetPopupWithYesNoString(TextController.GetStringWithId("uiCommonTextSell"), TextController.GetStringWithId("uiCommonTextNo"));
                        sc.SetEventOK(gameObject, "OnSellPopup");
                    }
                    break;
                case "Button_ItemListEquip":
                    {
                        Oc_ItemListItem lITem = collider.transform.parent.parent.GetComponent("Oc_ItemListItem") as Oc_ItemListItem;
                        m_SelectItem = lITem.m_Item;                       

                        Popup_TwoButton sc = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;
                        sc.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCharInfoEquipArmor"), TextController.GetStringWithId("uiPopupItemInventoryCaution1"));
                        sc.SetPopupWithYesNoString(TextController.GetStringWithId("uiCommonTextYes"), TextController.GetStringWithId("uiCommonTextNo"));
                        sc.SetEventOK(gameObject, "OnEquipPopup");
                    }
                    break;
                case "Button_LeftItemListPower":    // 왼손아이템 강화
                    {
                        ItemEnchantScene.SetMemberAndItem(charscene.m_SelectMember, charscene.m_SelectMember.leftItem);                      
                        OnStrengtheningButton();
                    }
                    break;
                case "Button_RightItemListPower":       // 오른손 아이템 강화
                    {
                        ItemEnchantScene.SetMemberAndItem(charscene.m_SelectMember, charscene.m_SelectMember.rightItem); 
                        OnStrengtheningButton();
                    }
                    break;
                case "Button_ItemListPower":        // 리스트 아이템 강화
                    {
                        ItemEnchantScene.SetMemberAndItem(null, m_SelectItem);
                        OnStrengtheningButton();
                    }
                    break;
            }
        }
    }

    void SetSortMenuUI()        // 소트 메뉴 라벨 설정
    {
        switch (m_SortKind)
        {
            case SortKind.Level:
                m_SortKindsLabel[0].text = tempString0;
                m_SortKindsLabel[1].text = tempString1;
                m_SortKindsLabel[2].text = tempString2;
                break;
            case SortKind.Rare:
                m_SortKindsLabel[0].text = tempString1;
                m_SortKindsLabel[1].text = tempString2;
                m_SortKindsLabel[2].text = tempString0;
                break;
            case SortKind.Inhance:
                m_SortKindsLabel[0].text = tempString2;
                m_SortKindsLabel[1].text = tempString0;
                m_SortKindsLabel[2].text = tempString1;
                break;
        }

        SortItem();       
    }


    void SortItem()
    {        
        m_SortedItem.Clear();
        foreach (Item t in UserData.Instance.userModel.inventory)   // 특성 맞는애들만 보여줌
        {
            if (t.itemInfo.itemType == (int)m_SortMode)
            {
                bool isAbleItem = false;

                foreach (int itemCalss in t.itemInfo.itemClass)
                {
                    if (!isAbleItem)
                    {
                        foreach (short trait in charscene.m_SelectMember.traits)
                        {
                            if (itemCalss == trait)
                            {
                                isAbleItem = true;
                                break;
                            }
                        }
                    }
                    else
                        break;
                }

                if (isAbleItem)
                    m_SortedItem.Add(t);
            }
        }        

        int max = m_SortedItem.Count;
       
        if (max > 1)        //2개 이상일때 소팅
        {
            for (int i = 0; i < max - 1; i++)
            {
                for (int j = 0; j < max - 1; j++)
                {
                    switch (m_SortKind)
                    {
                        case SortKind.Level:
                            {                           
                                if (m_SortedItem[j].itemInfo.level < m_SortedItem[j + 1].itemInfo.level)
                                {
                                    Item t = m_SortedItem[j];
                                    m_SortedItem[j] = m_SortedItem[j + 1];
                                    m_SortedItem[j + 1] = t;
                                }
                            }
                            break;
                        case SortKind.Rare:
                            {
                                if (m_SortedItem[j].itemInfo.rarity < m_SortedItem[j + 1].itemInfo.rarity)
                                {
                                    Item t = m_SortedItem[j];
                                    m_SortedItem[j] = m_SortedItem[j + 1];
                                    m_SortedItem[j + 1] = t;
                                }
                            }
                            break;
                        case SortKind.Inhance:
                            {
                                if (m_SortedItem[j].level < m_SortedItem[j + 1].level)
                                {
                                    Item t = m_SortedItem[j];
                                    m_SortedItem[j] = m_SortedItem[j + 1];
                                    m_SortedItem[j + 1] = t;
                                }
                            }
                            break;
                    }
                }
            }
        }

        //m_ItemTotalNum.text = ItemMax.ToString();
        m_ItemNum.text = max.ToString();
    }   

    void OnStrengtheningButton()
    {
        CharacterInfoScene.ModelForInitWithItemRoot(charscene.m_SelectMember);      // 현재 선택된 멤버 초기화 멤버로 등록
        SceneController.LoadLevel("Character_Item_Enchant");
    }


    void OnEquipPopup()      // 장착 확인 팝업 핸들러
    {        
        NetworkManager.Instance.C2S_ITEM_WEAR(UserData.Instance.userModel.inventory.IndexOf(m_SelectItem), charscene.m_SelectMember.no, OnItemEquipCallback);  
    }


    void OnItemEquipCallback(ResponseModel<UserModel> data)
    {
        if(data.ecode == 0)
        {
            SoundController.PlaySound("UI_item_equip_01");

            //데이터에 따라서 돈올려주는 것 필요할지도

            UserData.Instance.userModel = data.data;
            charscene.m_SelectMember = UserData.Instance.userModel.members_dic[charscene.m_SelectMember.no.ToString()];
            m_ItemPanelController.UpdateUI(true); // 전체 인포 갱신
            m_SelectListNum = -1;   //리스트 위치 갱신
            MoveList(true);

            if (data.responseResult.completeIdList.Count != 0)
            {
                foreach (int id in data.responseResult.completeIdList)
                {
                    ToastManager.Instance.ShowAchievementCompleteToast(id, false);
                }
            }

            Invoke("GetQuestInfo", 0.5f);


            ////////이곳에 추가
            //charscene.AttachWeapon(charscene.m_SelectMember, charscene.m_3DModel);
            //NetworkManager.Instance.C2S_GET_QUESTINFO(QuestManager.instance.ResponseGetQuestInfo);
        }
        else
        {
            Popup_OneButton popupSc = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;

            string errorId = string.Format("systemErrorText{0}", Math.Abs(data.ecode));

            popupSc.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(errorId));
        }  
    }



    void OnSellPopup()      // 판매 확인 팝업 핸들러
    {       
        int[] ids = new int[1] { UserData.Instance.userModel.inventory.IndexOf(m_SelectItem) };
        NetworkManager.Instance.C2S_ITEM_SELL(ids, OnItemSellCallback);    
    }

    void OnItemSellCallback(ResponseModel<UserModel> data)
    {
        if(data.ecode == 0)
        {
            //데이터에 따라서 돈올려주는 것 필요할지도

            SoundController.PlaySound("UI_gold");

            UserData.Instance.userModel = data.data;
            charscene.m_SelectMember = UserData.Instance.userModel.members_dic[charscene.m_SelectMember.no.ToString()];

            m_ItemPanelController.UpdateUI(true); // 전체 인포 갱신
            charscene.RefreshLeftListAndInfo();

            m_SelectListNum = -1;   //리스트 위치 갱신
            MoveList(true);

            if (data.responseResult.completeIdList.Count != 0)
            {
                foreach (int id in data.responseResult.completeIdList)
                {
                    ToastManager.Instance.ShowAchievementCompleteToast(id, false);
                }
            }

            Invoke("GetQuestInfo", 0.5f);
        }
        else
        {
            Popup_OneButton popupSc = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;

            string errorId = string.Format("systemErrorText{0}", Math.Abs(data.ecode));

            popupSc.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(errorId));
        }  
    }

    Vector3 m_ItemListScrollPrePos = Vector3.zero;
    void MoveList(bool isRefreshItem)
    {
        StopCoroutine("EndOffsetMoveToWithDelay");

        isAbleTouch = false;  // 터치 제한

        UIScrollView sv = GetComp("UIScrollView") as UIScrollView;
        UIPanel up = GetComp("UIPanel") as UIPanel;

        if (m_SelectListNum == -1)
        {
            m_IsOffsetMoving = true;
            sv.restrictWithinPanel = false;

            charscene.ChangeWepon(null);    // 3D모델 아이템 초기화

            for (int i = 0; i < m_ItemList.Count; i++)
            {
                m_ItemList[i].SetOn(false);
                //iTween.Stop(m_ItemList[i].gameObject);

                if (m_ItemList[i].gameObject.activeSelf)
                    iTween.MoveTo(m_ItemList[i].gameObject, iTween.Hash("islocal", true, "y", ScrollFirstItemOrizninY - ((i * ListGap)), "time", AniTime));               
                else
                {
                    Vector3 pos = m_ItemList[i].transform.localPosition;
                    pos.y = ScrollFirstItemOrizninY - ((i * ListGap));
                    m_ItemList[i].transform.localPosition = pos;
                }
            }

            //iTween.Stop(sv.gameObject);
            if (isRefreshItem)           
                iTween.MoveTo(sv.gameObject, iTween.Hash("islocal", true, "y", ScrollViewOrizinY, "time", AniTime)); 
            else
            {//m_ItemListScrollPrePos = sv.transform.localPosition;
                iTween.MoveTo(sv.gameObject, iTween.Hash("islocal", true, "y", m_ItemListScrollPrePos.y, "time", AniTime)); 
                
            }

            if (isEnable)
                StartCoroutine("EndOffsetMoveToWithDelay", AniTime + 0.2f);
            else
                EndOffsetMoveTo();

            m_SelectItem = null;
            UpdateItemInfoUI();
        }
        else
        {
            m_IsOffsetMoving = true;
            sv.restrictWithinPanel = false;

            m_ItemListScrollPrePos = sv.transform.localPosition;

            if (m_PreSelectListNum != m_SelectListNum)
            {
                for (int i = 0; i < m_ItemList.Count; i++)
                {
                    m_ItemList[i].SetOn(false);
                    
                    //iTween.Stop(m_ItemList[i].gameObject);

                    if (i > m_SelectListNum)
                    {

                        if (m_ItemList[i].gameObject.activeSelf)
                            iTween.MoveTo(m_ItemList[i].gameObject, iTween.Hash("islocal", true, "y", ScrollFirstItemOrizninY - ((i * ListGap) + PopupGap), "time", AniTime));
                        else
                        {
                            Vector3 pos = m_ItemList[i].transform.localPosition;
                            pos.y = ScrollFirstItemOrizninY - ((i * ListGap) + PopupGap);
                            m_ItemList[i].transform.localPosition = pos;
                        }
                    }
                    else
                    {
                        if (m_ItemList[i].gameObject.activeSelf)
                            iTween.MoveTo(m_ItemList[i].gameObject, iTween.Hash("islocal", true, "y", ScrollFirstItemOrizninY - ((i * ListGap)), "time", AniTime));
                        else
                        {
                            Vector3 pos = m_ItemList[i].transform.localPosition;
                            pos.y = ScrollFirstItemOrizninY - ((i * ListGap));
                            m_ItemList[i].transform.localPosition = pos;
                        }
                    }
                }

                StartCoroutine("PopMenu");

                //iTween.Stop(sv.gameObject);
                iTween.MoveTo(sv.gameObject, iTween.Hash("islocal", true, "y", ScrollViewOrizinY + (ListGap * m_SelectListNum), "time", AniTime, "oncompletetarget", gameObject, "oncomplete", "EndOffsetMoveTo"));

                m_SelectItem = m_ItemList[m_SelectListNum].m_Item;
                UpdateItemInfoUI();
            }
        }
    }

    IEnumerator PopMenu()
    {
        yield return new WaitForSeconds(AniTime);

        m_ItemList[m_SelectListNum].SetOn(true);

        charscene.ChangeWepon(m_ItemList[m_SelectListNum].m_Item);   // 선택 아이템으로 3D모델 아이템 설정
    }

    // 인포 패널에 보여지는 순서
    AptitudeID_New[] m_ShowOrder = new AptitudeID_New[7]
    {
        AptitudeID_New.atkType,
        AptitudeID_New.baseAtk,
        AptitudeID_New.taunt,
        AptitudeID_New.init,
        AptitudeID_New.baseDef,
        AptitudeID_New.assist,
        AptitudeID_New.baseHP            
    };

    List<int> m_ItemEffectTypeList = new List<int>();


    string[] atkName = new string[5] { "combatStatTextPhyAttack", "combatStatTextMagAttack", 
                    "combatStatTextPhyAttack", "combatStatTextPhyAttack", "combatStatTextPhyAttack" };

    void UpdateItemInfoUI()
    {
        charscene.m_SelectMember.CalcUnitStat();
        UnitStat tempStat = charscene.m_SelectMember.m_StatWithItem;
        DBaseCharacter_New stats = tempStat.data;


        m_ItemEffectTypeList.Clear();   // 빅버튼 필요한 스텟이 몇번인지 찾음
        if (m_SelectItem != null)
        {
            m_ItemEffectTypeList.Add(m_SelectItem.itemInfo.effectType1);
            m_ItemEffectTypeList.Add(m_SelectItem.itemInfo.effectType2);

            if (charscene.m_SelectMember.leftItem != null)
            {
                if (m_SelectItem.itemInfo.part == charscene.m_SelectMember.leftItem.itemInfo.part)
                {                    
                    int effID = charscene.m_SelectMember.leftItem.itemInfo.effectType1;

                    if (!m_ItemEffectTypeList.Contains(effID))
                        m_ItemEffectTypeList.Add(effID);

                    effID = charscene.m_SelectMember.leftItem.itemInfo.effectType2;
                    if (!m_ItemEffectTypeList.Contains(effID))
                        m_ItemEffectTypeList.Add(effID);
                }
            }

            if (charscene.m_SelectMember.rightItem != null)
            {
                if (m_SelectItem.itemInfo.part == charscene.m_SelectMember.rightItem.itemInfo.part)
                {
                    int effID = charscene.m_SelectMember.rightItem.itemInfo.effectType1;

                    if (!m_ItemEffectTypeList.Contains(effID))
                        m_ItemEffectTypeList.Add(effID);

                    effID = charscene.m_SelectMember.rightItem.itemInfo.effectType2;
                    if (!m_ItemEffectTypeList.Contains(effID))
                        m_ItemEffectTypeList.Add(effID);
                }
            }
        }

      
        List<int> bigIndex = new List<int>();
        foreach (int effID in m_ItemEffectTypeList)    // 빅 패널 앞뒤로 띄우기 인덱스 조사
        {
            int index = -1;
            for (int j = 0; j < m_ShowOrder.Length; j++)
            {
                if(effID == (int)m_ShowOrder[j])
                {
                    index = j;
                    break;
                }
            }

            if(index != -1)
            {
                bigIndex.Add(index);
                bigIndex.Add(index + 1);
            }
        }

        float lineGap = -StatLabelLineGapY;    

        float[] posList = new float[m_ShowOrder.Length];
        for (int i = 0; i < m_ShowOrder.Length; i++)
        {
            if (m_StatLabels[i] == null)
            {
                Transform root = m_StatRoot.Find(string.Format("Stat{0:00}", i + 1)).Find("off");
                m_StatLabels[i] = root.Find("value").GetComponent("UILabel") as UILabel;
                m_StatNameLabels[i] = root.Find("text").GetComponent("UILabel") as UILabel;
            }

            if (i == 0)
            {
                m_StatLabels[i].text = string.Format("{0:#,0}", TextController.GetStringWithId(atkName[stats.atkType - 1]));
            }
            else
            {              
                m_StatLabels[i].text = string.Format("{0:#,0}", stats[m_ShowOrder[i]]);

                //MemberModel tempModel = charscene.m_SelectMember;

                //int effValue = 0;
                //if (tempModel.leftItem != null)
                //{
                //    if (UnitStat.EffectIdToStatIndex(tempModel.leftItem.itemInfo.effectType1) == i)
                //        effValue += tempModel.leftItem.effectValue01;

                //    if (UnitStat.EffectIdToStatIndex(tempModel.leftItem.itemInfo.effectType2) == i)
                //        effValue += tempModel.leftItem.effectValue02;
                //}

                //if (tempModel.rightItem != null)
                //{
                //    if (UnitStat.EffectIdToStatIndex(tempModel.rightItem.itemInfo.effectType1) == i)
                //        effValue += tempModel.rightItem.effectValue01;

                //    if (UnitStat.EffectIdToStatIndex(tempModel.rightItem.itemInfo.effectType2) == i)
                //        effValue += tempModel.rightItem.effectValue02;
                //}

                //if (effValue > 0)
                //{
                //    m_StatLabels[i].text = string.Format("{0}  [55cff9](+{1})[-]", m_StatLabels[i].text, effValue);
                //}
            }          

            

            if (bigIndex.Contains(i))
            {
                int numOfPlus = 0;
                foreach (int t in bigIndex)
                {
                    if (t == i)
                    {
                        lineGap += (StatLabelBigGapY - (numOfPlus * StatLabelBigGapY / 2)); // 연속 큰 라벨은 사이값 조정
                        numOfPlus++;
                    }
                }                
            }           
                
            lineGap += StatLabelLineGapY;

            posList[i] = StatLabelStartY - lineGap;           
        }

        int bigNum = 0;
        for (int i = 0; i < m_ShowOrder.Length; i++)
        {
            if (m_ItemEffectTypeList.Contains((int)m_ShowOrder[i]))
            {                
                Vector3 pos = m_BigStatLabels[bigNum].transform.localPosition;          // 큰 패널 작은 패널의 위치로 이동후 애니메이션 구동
                pos.y = m_StatLabels[i].transform.parent.parent.localPosition.y;
                m_BigStatLabels[bigNum].transform.localPosition = pos;
                m_BigStatLabels[bigNum].SetActive(true);

                pos = m_StatLabels[i].transform.parent.parent.localPosition;    // 작은 패널 좌표는 미리 이동시켜 놓는다 (추후 애니메이션 위해서)
                pos.y = posList[i];
                m_StatLabels[i].transform.parent.parent.localPosition = pos;
                m_StatLabels[i].transform.parent.parent.gameObject.SetActive(false);
                //
                SetBigStatPanel(m_BigStatLabels[bigNum], bigNum, (int)m_ShowOrder[i], m_StatNameLabels[i].text); // 빅 스텟 패널 세팅

                iTween.MoveTo(m_BigStatLabels[bigNum],
                    iTween.Hash("islocal", true, "y", posList[i], "time", AniTime));                
               
                bigNum++;
            }
            else
            {
                m_StatLabels[i].transform.parent.parent.gameObject.SetActive(true);               

                iTween.MoveTo(m_StatLabels[i].transform.parent.parent.gameObject,
                    iTween.Hash("islocal", true, "y", posList[i], "time", AniTime));
            }
        }

        for (int i = bigNum; i < m_BigStatLabels.Length ; i++)
        {
            m_BigStatLabels[i].SetActive(false);
        }

        // 배경 에니 추가        
        if (isEnable)
        {
            m_IsStatBGMoving = true;
            StopCoroutine("StopStatBGMove");
            StartCoroutine("StopStatBGMove", AniTime + 0.2f);
        }
        else
        {
            StatBGMove();
        }
    }


    

    void SetBigStatPanel(GameObject bigRoot, int order, int effetType, string effName)
    {        
        Item crrItem = null;

        if (charscene.m_SelectMember.leftItem != null)
        {
            if (m_SelectItem.itemInfo.part == charscene.m_SelectMember.leftItem.itemInfo.part)
                crrItem = charscene.m_SelectMember.leftItem;
            else
                crrItem = charscene.m_SelectMember.rightItem;
        }
        else if (charscene.m_SelectMember.rightItem != null)
        {
            if (m_SelectItem.itemInfo.part == charscene.m_SelectMember.rightItem.itemInfo.part)
                crrItem = charscene.m_SelectMember.rightItem;        
        }       

        UnitStat tempStat = charscene.m_SelectMember.m_Stat;
        DBaseCharacter_New stats = tempStat.data;

        UILabel tempLabel = null;
        int charValue = 0;
        int itemValue = 0;
        int crrItemValue = 0;

        charValue =  Convert.ToInt32(stats[(AptitudeID_New)effetType]);

        if (m_SelectItem.itemInfo.effectType1 == effetType)
            itemValue = m_SelectItem.effectValue01;
        else if (m_SelectItem.itemInfo.effectType2 == effetType)
            itemValue = m_SelectItem.effectValue02;

        if (crrItem == null)
            crrItemValue = 0;
        else
        {
            if (crrItem.itemInfo.effectType1 == effetType)
                crrItemValue = crrItem.itemInfo.effectValue1;
            else if (crrItem.itemInfo.effectType2 == effetType)
                crrItemValue = crrItem.itemInfo.effectValue2;
        }

        tempLabel = bigRoot.transform.Find("traitName").GetComponent("UILabel") as UILabel;
        tempLabel.text = effName;
        //tempLabel.text = string.Format("{0:#,0}", stats[(AptitudeID_New)effetType]);

        tempLabel = bigRoot.transform.Find("traitValue").GetComponent("UILabel") as UILabel;
        tempLabel.text = string.Format("{0:#,0} [55cff9]", charValue);

        int gap = itemValue - crrItemValue;

        if (gap < 0)
        {
            bigRoot.transform.Find("up").gameObject.SetActive(false);
            Transform root = bigRoot.transform.Find("down");
            root.gameObject.SetActive(true);

            tempLabel = root.Find("value").GetComponent("UILabel") as UILabel;
            tempLabel.text =  Math.Abs(gap).ToString();
        }
        else
        {           
            bigRoot.transform.Find("down").gameObject.SetActive(false);

            Transform root = bigRoot.transform.Find("up");
            root.gameObject.SetActive(true);

            tempLabel = root.Find("value").GetComponent("UILabel") as UILabel;
            tempLabel.text = gap.ToString();
        }
    }

    IEnumerator StopStatBGMove(float time)
    {
        yield return new WaitForSeconds(time);

        m_IsStatBGMoving = false;
    }

    public override void UpdateObject()
    {
        if (m_IsOffsetMoving)
            OffsetMove();

        if (m_IsStatBGMoving)
            StatBGMove();
    }


    void StatBGMove()
    {
        m_StatBG.height = (int)m_StatLabels[0].transform.parent.parent.localPosition.y - (int)m_StatLabels[6].transform.parent.parent.localPosition.y + 100;

        Vector3 pos = m_StatBG.transform.localPosition;
        pos.y = m_StatLabels[0].transform.parent.parent.localPosition.y - (m_StatBG.height / 2.0f) + 50;
        m_StatBG.transform.localPosition = pos;
    }

    bool isEnable = false;
    void OnEnable()
    {
        isEnable = true;

        MoveList(true);
    }

    void OnDisable()
    {
        isEnable = false;
    }

    void GetQuestInfo()
    {
        NetworkManager.Instance.C2S_GET_QUESTINFO(QuestManager.instance.ResponseGetQuestInfo);
    }
}